package com.wdb007.venus.svc.biz.account;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wdb.exchange.constant.EventEnum;
import com.wdb007.venus.svc.biz.manager.ConfigInfoManager;
import com.wdb007.venus.svc.biz.manager.IntersystemCommunicationManager;
import com.wdb007.venus.svc.biz.utils.DateFormatUtil;
import com.wdb007.venus.svc.biz.utils.DateUtil;
import com.wdb007.venus.svc.dal.account.dao.AccountCashDAO;
import com.wdb007.venus.svc.dal.account.dao.AccountCashLogDAO;
import com.wdb007.venus.svc.dal.account.model.AccountCashDO;
import com.wdb007.venus.svc.dal.account.model.AccountCashLogDO;
import com.wdb007.venus.svc.dal.account.model.AccountCashLogQueryDO;
import com.wdb007.venus.svc.dal.account.model.ExpenseRecordDO;
import com.wdb007.venus.svc.dal.user.dao.UserInfoDAO;
import com.wdb007.venus.svc.facade.AccountCashService;
import com.wdb007.venus.svc.facade.WxAPPMessagePushService;
import com.wdb007.venus.svc.facade.account.model.AccountCashDTO;
import com.wdb007.venus.svc.facade.account.model.AccountCashLogDTO;
import com.wdb007.venus.svc.facade.account.model.AccountCashLogVO;
import com.wdb007.venus.svc.facade.account.model.ChargeAmount;
import com.wdb007.venus.svc.facade.account.model.ChargeAmountVO;
import com.wdb007.venus.svc.facade.book.model.UserBorrowPageDTO;
import com.wdb007.venus.svc.facade.exception.BizException;
@Service("accountCashService")
public class AccountCashServiceImpl implements AccountCashService {

	@Autowired
	private AccountCashDAO accountCashDAO;
	
	@Autowired
	private AccountCashLogDAO accountCashLogDAO;
	
	@Autowired
	private ConfigInfoManager configInfoManager;

	@Autowired
	private WxAPPMessagePushService wxAPPMessagePushService;
	
	@Autowired
	private UserInfoDAO userInfoDAO;
	
	@Autowired
	private IntersystemCommunicationManager intersystemCommunicationManager;
	@Override
	public boolean isAdequate(Long userid)  throws BizException{
		
		AccountCashDO accountCashDO = accountCashDAO.getByUserid(userid);
		if(accountCashDO != null && accountCashDO.getCashAmount().doubleValue() >= 1.0) {
			return true ;
		}
		return false;
	}

	@Override
	public boolean isDeposit(Long userid)  throws BizException{
		AccountCashDO accountCashDO = accountCashDAO.getByUserid(userid);
		if(accountCashDO != null && accountCashDO.getDepositAmount().doubleValue() > 0) {
			return true ;
		}
		return false;
	}

	@Override
	public AccountCashLogVO getChargeList(UserBorrowPageDTO userBorrowPageDTO) throws BizException{
		Long userid = userBorrowPageDTO.getUserid();
		
		AccountCashLogVO vo = new AccountCashLogVO();
		//1.首先查询充值记录是否为0，也就是没有进行过充值记录
		int chargeCount = accountCashLogDAO.countByUser(userid);
		vo.setTotalcount(chargeCount);
		if(chargeCount == 0) {
			return vo;
		}
		AccountCashLogQueryDO queryDO = new AccountCashLogQueryDO();
		queryDO.setOffset(userBorrowPageDTO.getOffset());
		queryDO.setPagesize(userBorrowPageDTO.getPagesize());
		queryDO.setUserid(userid);
		List<ExpenseRecordDO> list = accountCashLogDAO.getByPageQuery(queryDO);
		if(CollectionUtils.isEmpty(list)) {
			return null ;
		}
		List<AccountCashLogDTO> dtoList = new ArrayList<AccountCashLogDTO>();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		for(int i=0; i<list.size(); i++) {
			ExpenseRecordDO logDO = list.get(i);
			AccountCashLogDTO dto = new AccountCashLogDTO();
			Integer operType = logDO.getOperType();
			Integer chargeType = logDO.getChargeType();
			Date createTime = logDO.getCreateTime();
			dto.setChargeTime(df.format(createTime));
			dto.setCurrency("CNY");
			dto.setBookName("");
			dto.setBorrowPeriod("");
			if(10 == operType) {
				dto.setOperType("充值");
				dto.setChargeAmount("+" + logDO.getChangeAmount().toString());
			}else if(20 == operType){
				dto.setOperType("消费");
				dto.setChargeAmount("-" + logDO.getChangeAmount().toString());
			}else if(30 == operType) {
				dto.setOperType("退款");
				dto.setChargeAmount("-" + logDO.getChangeAmount().toString());
			}
			switch (chargeType) {
				case 10:
					dto.setChargeType("微信");
					break;
				case 20:
					dto.setChargeType("支付宝");
					break;
				case 30:
					dto.setChargeType("系统赠送");
					break;
				case 50:
					dto.setOperType("借阅");
					dto.setBookName(logDO.getBookName());
					dto.setChargeTime(df.format(logDO.getModifyTime()));
					dto.setBorrowPeriod(DateUtil.getDatePoor(logDO.getModifyTime(), logDO.getCreateTime()));
					break;
				case 51:
					dto.setChargeType("购买会员");
					break;
				case 52:
					dto.setChargeType("购买绘本");
					break;
				case 60:
					dto.setChargeType("充值劵赠送");
					break;
				case 70:
					dto.setChargeType("VIP充值赠送");
					break;
				case 80:
					dto.setChargeType("邀请赠送");
					break;
				case 90:
					dto.setChargeType("活动赠送");
					break;
				default:
					break;
			}
			dtoList.add(dto);
		}
		vo.setList(dtoList);
		return vo;
	}

	@Override
	public ChargeAmountVO getChargeDepositAmountList()  throws BizException{
		
		ChargeAmountVO voDeposit = new ChargeAmountVO();

		voDeposit.getCharges().add(new ChargeAmount("100_99",configInfoManager.getValue("100"),configInfoManager.getValue("99")));
		voDeposit.getCharges().add(new ChargeAmount("50_99",configInfoManager.getValue("50"),configInfoManager.getValue("99")));
		voDeposit.getCharges().add(new ChargeAmount("30_99",configInfoManager.getValue("30"),configInfoManager.getValue("99")));
		voDeposit.getCharges().add(new ChargeAmount("10_99",configInfoManager.getValue("10"),configInfoManager.getValue("99")));
		
		return voDeposit;
	}
	@Override
	public ChargeAmountVO getChargeOnlyAmountList() throws BizException {
		ChargeAmountVO voOnlyAmount = new ChargeAmountVO();
		voOnlyAmount.getCharges().add(new ChargeAmount("100_0",configInfoManager.getValue("100"),configInfoManager.getValue("0")));
		voOnlyAmount.getCharges().add(new ChargeAmount("50_0",configInfoManager.getValue("50"),configInfoManager.getValue("0")));
		voOnlyAmount.getCharges().add(new ChargeAmount("30_0",configInfoManager.getValue("30"),configInfoManager.getValue("0")));
		voOnlyAmount.getCharges().add(new ChargeAmount("10_0",configInfoManager.getValue("10"),configInfoManager.getValue("0")));
		return voOnlyAmount;
	}
	@Override
	public ChargeAmount getChargeByCode(String chargeCode) throws BizException {
		ChargeAmountVO voDeposit = this.getChargeDepositAmountList();
		ChargeAmountVO voOnlyAmount = this.getChargeOnlyAmountList();
		for(ChargeAmount chargeAmount:voDeposit.getCharges()) {
			if(chargeCode.equals(chargeAmount.getCharge_code())) {
				return chargeAmount;
			}
		}
		for(ChargeAmount chargeAmount:voOnlyAmount.getCharges()) {
			if(chargeCode.equals(chargeAmount.getCharge_code())) {
				return chargeAmount;
			}
		}
		return null;
	}

	@Override
	public int updateAccountCash(AccountCashDTO acDTO) throws BizException {
		
		AccountCashDO accountCash = accountCashDAO.getByUserid(acDTO.getUserId());
		if(null == accountCash) {	//说明是注册时使用
			//FIXME 注册用户处理
		}
		Long accountCashId = accountCash.getId();
		if(acDTO.isDepositFlag()) { //押金充值
			accountCash.setDepositAmount(acDTO.getChangeAmount());
		}else if(10 == acDTO.getOperType()) {	//充值
			accountCash.setCashAmount(accountCash.getCashAmount().add(acDTO.getChangeAmount()));
		}else {	//扣费
			accountCash.setCashAmount(accountCash.getCashAmount().subtract(acDTO.getChangeAmount()));
		}
		AccountCashLogDO aclDO = new AccountCashLogDO();
		aclDO.setAccountCashId(accountCashId);
		aclDO.setOrderId(acDTO.getOrderId());
		aclDO.setChangeAmount(acDTO.getChangeAmount());
		aclDO.setOperType(acDTO.getOperType());
		aclDO.setChargeType(acDTO.getChargeType());
		aclDO.setCreateTime(new Date());
		accountCash.setModifyTime(new Date());
		
		String mobileNo = userInfoDAO.get(acDTO.getUserId()).getMobileNo();
		//押金充值
		if(acDTO.isDepositFlag()) {
			String deposit_amount = configInfoManager.getValue("deposit_amount");
			if(acDTO.getChangeAmount().compareTo(new BigDecimal(deposit_amount)) > 0) {
				accountCash.setCashAmount(acDTO.getChangeAmount().subtract(new BigDecimal(deposit_amount)));
				accountCash.setDepositAmount(new BigDecimal(deposit_amount));
			}
			wxAPPMessagePushService.sendPayDepositNotice(acDTO.getChangeAmount(), "在线支付", DateFormatUtil.formatDate2(new Date()), acDTO.getUserId());
			intersystemCommunicationManager.publishEvent(acDTO.getUserId(), EventEnum.DEPOSIT);
		}else if(10 == acDTO.getOperType()){ //余额充值
			wxAPPMessagePushService.sendRechangeToAccountNotice(mobileNo, acDTO.getChargeType(), new Date(), acDTO.getChangeAmount(), acDTO.getUserId());
			intersystemCommunicationManager.publishEvent(acDTO.getUserId(), EventEnum.CHARGE);
		}
		
		accountCashDAO.update(accountCash);
		accountCashLogDAO.insert(aclDO);
		return 1;
	}

	@Override
	public BigDecimal getChargeAmount(Long chargeLogId){
		
		BigDecimal changAmount = accountCashLogDAO.getChangeAmount(chargeLogId);
		
		return changAmount;
	}
}
